package me.xue.communityhealth.service;

import com.github.pagehelper.Page;
import me.xue.communityhealth.exception.ObjectNotFoundException;
import me.xue.communityhealth.model.Fee;
import me.xue.communityhealth.repo.FeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class FeeService implements BaseService<Fee, Integer> {

    private final FeeRepository feeRepository;

    @Autowired
    public FeeService(FeeRepository feeRepository) {
        this.feeRepository = feeRepository;
    }

    @Override
    public int save(Fee fee) {
        return feeRepository.save(fee);
    }

    @Override
    public int update(Fee fee) {
        return feeRepository.update(fee);
    }

    @Override
    public int delete(Integer which) {
        return feeRepository.delete(which);
    }

    @Override
    public List<Fee> findAll() {
        return null;
    }

    @Override
    public Page<Fee> pagedAll() {
        return feeRepository.pagedFees();
    }

    public List<Fee> findFeesByName(String name) {
        if (StringUtils.isEmpty(name)) {
            return new ArrayList<>(0);
        }
        return feeRepository.findByName(name);
    }

    public Fee findFee(Integer id) {
        Fee fee = feeRepository.findOne(id);
        if (fee == null) {
            throw new ObjectNotFoundException();
        }
        return fee;
    }
}
